#include <iostream>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "RingQueue.hpp"

struct threaddata{
    RingQueue<int> *rq;
    std::string name;
};

int _data = 1;

void *consumer(void *args)
{
    threaddata* td = static_cast<threaddata *>(args);
    while (1)
    {
        int * d ;
        td->rq->Pop(d);
        std::cout << td->name << " 消费了一个任务: " << *d << std::endl;
    }
}
void *product(void *args)
{
    threaddata* td = static_cast<threaddata *>(args);

    while (1)
    {
        std::cout << td->name << " 生产了一个任务: " << _data << std::endl;
        td->rq->Equeue(_data);
        _data++;
    }
}

int main()
{

    pthread_t c[2],p[3];
    RingQueue<int> *rq = new RingQueue<int>();
    threaddata* t1 = new threaddata();
    t1->name="thread 1";
    t1->rq = rq;
    pthread_create(c, nullptr, consumer, (void *)t1);

    threaddata* t2 = new threaddata();
    t2->name="thread 2";
    t2->rq = rq;
    pthread_create(c+1, nullptr, consumer, (void *)t2);

    threaddata* t3 = new threaddata();
    t3->name="thread 3";
    t3->rq = rq;
    pthread_create(p, nullptr, product, (void *)t3);
    
    threaddata* t4 = new threaddata();
    t4->name="thread 4";
    t4->rq = rq;
    pthread_create(p+1, nullptr, product, (void *)t4);

    threaddata* t5 = new threaddata();
    t5->name="thread 5";
    t5->rq = rq;
    pthread_create(p+2, nullptr, product, (void *)t5);

    pthread_join(*c,nullptr);
    pthread_join(*(c+1),nullptr);
    pthread_join(*p,nullptr);
    pthread_join(*(p+1),nullptr);
    pthread_join(*(p+2),nullptr);

    return 0;
}